package cn.iocoder.yudao.module.resource.service.story;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.resource.controller.admin.story.vo.*;
import cn.iocoder.yudao.module.resource.dal.dataobject.story.StoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.resource.dal.mysql.story.StoryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.resource.enums.ErrorCodeConstants.*;

/**
 * 场景 Service 实现类
 *
 * @author lizhongwen
 */
@Service
@Validated
public class StoryServiceImpl implements StoryService {

    @Resource
    private StoryMapper storyMapper;

    @Override
    public Integer createStory(StorySaveReqVO createReqVO) {
        // 插入
        StoryDO story = BeanUtils.toBean(createReqVO, StoryDO.class);
        storyMapper.insert(story);
        // 返回
        return story.getId();
    }

    @Override
    public void updateStory(StorySaveReqVO updateReqVO) {
        // 校验存在
        validateStoryExists(updateReqVO.getId());
        // 更新
        StoryDO updateObj = BeanUtils.toBean(updateReqVO, StoryDO.class);
        storyMapper.updateById(updateObj);
    }

    @Override
    public void deleteStory(Integer id) {
        // 校验存在
        validateStoryExists(id);
        // 删除
        storyMapper.deleteById(id);
    }

    private void validateStoryExists(Integer id) {
        if (storyMapper.selectById(id) == null) {
            throw exception(STORY_NOT_EXISTS);
        }
    }

    @Override
    public StoryDO getStory(Integer id) {
        return storyMapper.selectById(id);
    }

    @Override
    public PageResult<StoryDO> getStoryPage(StoryPageReqVO pageReqVO) {
        return storyMapper.selectPage(pageReqVO);
    }

}